"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Pluginsify = void 0;
const lodash_1 = require("lodash");
const utils_1 = require("@umijs/utils");
const getUmiService = require('cpaas-webpack-scripts/scripts/umi-scripts/umiService')
    .getUmiService;
function padLeft(str) {
    return str
        .split('\n')
        .map((line) => `    ${line}`)
        .join('\n');
}
class Pluginsify {
    constructor() {
        this.cliService = getUmiService({
            plugins: [require.resolve('./umi-commands/upgrade')],
        });
    }
    load(program) {
        this.cliService.init();
        // this.cliService.run({
        //   name: 'cpaas-start',
        // }) as any;
        const cpaasCliCommands = lodash_1.omit(this.cliService.commands, [
            'build',
            'config',
            'dev',
            'generate',
            'help',
            'plugin',
            'version',
            'webpack',
            'cpaas-start',
            'g',
        ]);
        const cpaasCliCommandsKeys = Object.keys(cpaasCliCommands);
        cpaasCliCommandsKeys.forEach((commandName) => {
            const commandObj = cpaasCliCommands[commandName];
            if (commandObj === undefined || typeof commandObj === 'string') {
                return;
            }
            // const opts = commands[name].opts || {};
            const details = commandObj.details
                ? `\n  Details:\n${padLeft(commandObj.details)}\n`
                : '';
            const cmd = program
                .command(commandObj.name)
                .description(`${commandObj.description || ''}${details}`);
            const alias = cpaasCliCommandsKeys.find((key) => {
                const commandObj2 = cpaasCliCommands[key];
                return typeof commandObj2 === 'string' && commandObj2 === commandName;
            });
            // for (const [flags, description] of Object.entries(opts.options || {})) {
            //   cmd.option(flags, `${description || ''}`);
            // }
            if (alias) {
                cmd.alias(alias);
            }
            cmd.action((command) => __awaiter(this, void 0, void 0, function* () {
                // await this.action.handle(inputs, options);
                // console.log(command);
                const args = utils_1.yParser(process.argv.slice(2));
                yield this.cliService.runCommand({ name: commandObj.name, args });
            }));
            // cmd.help((helpMsg) => {
            //   console.log(helpMsg);
            //   if(!commandObj.details) {
            //     return '';
            //   }
            //   return `Details:\n${padLeft(commandObj.details)}\n`;
            // });
        });
    }
}
exports.Pluginsify = Pluginsify;
